package cn.iocoder.yudao.module.vote.service.voteuserresult.strategy;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.iocoder.yudao.module.vote.constants.ScopeTypeEnum;
import cn.iocoder.yudao.module.vote.constants.VotePersonTypeEnum;
import cn.iocoder.yudao.module.vote.constants.VoteReviewerTypeEnum;
import cn.iocoder.yudao.module.vote.controller.admin.voterecorddetail.vo.VoteRecordCountVO;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteuserresult.VoteUserResultDO;
import cn.iocoder.yudao.module.vote.dal.mysql.voterecorddetail.VoteRecordDetailMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import static cn.iocoder.yudao.module.vote.constants.ScopeTypeEnum.*;
import static cn.iocoder.yudao.module.vote.constants.VotePersonTypeEnum.getVotePersonType;

/**
 *
 */
@Component
public class PartyLeadersScopeStrategy implements ScopeStrategy{
    @Resource
    private VoteRecordDetailMapper recordDetailMapper;

    @Override
    public BigDecimal totalScope(Integer voteId, Integer userResultId,Integer type) {
        List<VoteRecordCountVO> otherLeadersList = recordDetailMapper.getByResultId(voteId, userResultId, getVotePersonType(type).getValue());

        if (CollUtil.isEmpty(otherLeadersList)) {
            return BigDecimal.ZERO;
        }

        double totalScore = 0.0D;
        for (VoteRecordCountVO recordCountVO : otherLeadersList) {
            totalScore += recordCountVO.getKeyTasks()+recordCountVO.getCollaboration()+recordCountVO.getProgress()+recordCountVO.getTeamBuilding();
        }
        double averageScore = NumberUtil.div(totalScore, otherLeadersList.size() * 1.0D);
        double weightedScore = getVotePersonType(type).getWeight() * averageScore;

        return new BigDecimal(weightedScore).setScale(2, RoundingMode.HALF_UP);
    }

    @Override
    public void avgScope(VoteUserResultDO resultDO, Integer voteId){

        Integer userResultId = resultDO.getId();
        BigDecimal otherLeaders = totalScope(voteId,userResultId, VotePersonTypeEnum.OTHER_LEADERS.getValue());
        BigDecimal partyLeaders = totalScope(voteId,userResultId, VotePersonTypeEnum.PARTY_LEADER.getValue());
        // 被投票人是组长
        if(VoteReviewerTypeEnum.TEAM_LEADER.getValue() ==resultDO.getVoteReviewerType()){
            BigDecimal teamMembers = totalScope(voteId,userResultId, VotePersonTypeEnum.TEAM_MEMBER.getValue());
            resultDO.setTeamMemberScore(teamMembers.doubleValue());
            resultDO.setAvgScore(NumberUtil.add(otherLeaders,partyLeaders,teamMembers).floatValue());
        }else{
            BigDecimal teamLeaders = totalScope(voteId,userResultId, VotePersonTypeEnum.TEAM_LEADER.getValue());
            resultDO.setTeamLeaderScore(teamLeaders.doubleValue());
            resultDO.setAvgScore(NumberUtil.add(otherLeaders,partyLeaders,teamLeaders).floatValue());
        }
        resultDO.setOtherLeaderScore(otherLeaders.doubleValue());
        resultDO.setPartyLeaderScore(partyLeaders.doubleValue());
    }



}
